package com.stuinfo;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SelectWay extends JDialog implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SelectShow selectshow;
	private JTextField dangechaxuntf;
	private JTextField[] showtf = new JTextField[3];

	public SelectWay(Frame jfr, boolean b) {
		super(jfr, b);
		JPanel pan = new JPanel();
		pan.setLayout(new GridBagLayout());

		String[] sebt = { "单个查询", "全部查询" };
		String[] selb = { "根据学号查询单个学生信息", "查询全部学生信息" };
		String[] str = { "学号", "姓名", "性别" };
		
		GridBagConstraints gbc = new GridBagConstraints();
		
		dangechaxuntf = new JTextField(5);
		gbc.gridx = 2;
		gbc.gridy = 0;
		pan.add(dangechaxuntf, gbc);
		for (int i = 0; i < sebt.length; i++) {
			JLabel lb = new JLabel(selb[i]);
			gbc.gridx = 0;
			gbc.gridy = i;
			pan.add(lb, gbc);
			
			JButton jb = new JButton(sebt[i]);
			gbc.gridx = 3;
			gbc.gridy = i;
			gbc.insets = new Insets(20, 0, 20, 0);
			pan.add(jb, gbc);
			jb.addActionListener(this);
		}

		for (int i = 0; i < str.length; i++) {
			JLabel la = new JLabel(str[i]);
			gbc.gridx = 0;
			gbc.gridy = 6 + i;
			pan.add(la, gbc);
			gbc.insets = new Insets(20, 10, 20, 10);

			showtf[i] = new JTextField(20);
			gbc.gridx = 2;
			gbc.gridy = 6+ i;
			showtf[i].setEnabled(false);
			pan.add(showtf[i], gbc);

		}

    	add(pan);

		selectshow = new SelectShow(this, true);
		selectshow.setSize(700, 550);
		selectshow.setLocation(200, 100);

	}

	public void actionPerformed(ActionEvent e) {

		String cmd = e.getActionCommand();
		switch (cmd) {
		
		case "单个查询":
			System.out.println("***"+dangechaxuntf.getText());
			waySelect(MainInfo.getConn());
			break;
			
		case "全部查询":
			selectshow.allSelect(MainInfo.conn);
			this.setVisible(false);
			selectshow.setVisible(true);  
			break;
		}

	}
	
	public void waySelect(Connection conn) {
		String sql_select = "select * from stuinfo where id=?";

		try {
			PreparedStatement pre_select = conn.prepareStatement(sql_select);
			System.out.println("请输入想查询学生ID号");
			
			pre_select.setInt(1, Integer.valueOf(dangechaxuntf.getText()));
			ResultSet res = pre_select.executeQuery();
			res.next();
       
			showtf[0].setText(String.valueOf(res.getInt("id")));
			showtf[1].setText(res.getString("name"));
			showtf[2].setText(res.getString("sex"));
			
			
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(this, "输入的学号不存在，请重新输入");
			dangechaxuntf.setText("");
		}
		catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "ID必须为数字，请重新输入");
			dangechaxuntf.setText("");
		}
for (JTextField jTextField : showtf) {
	System.out.println();
	
}
	}

	

	
	
}
